<?php

namespace app\admin\controller\vpay;

use app\common\controller\Backend;
use app\admin\model\Config as ConfigModel;

/**
 * 客户端
 */
class Config extends Backend
{
    protected object $model;
    public function initialize(): void
    {
        parent::initialize();
        $this->model = new ConfigModel();
    }

    public function index(): void
    {
        $config= get_sys_config('','vpay');
        $this->success('', $config);
    }

    public function edit(): void
    {
        if (!$this->request->isPost()) {
            $this->error(__('Parameter error'));
        }
        $data = $this->request->post();
        if (!$data) {
            $this->error(__('Parameter %s can not be empty', ['']));
        }
        foreach ($data as $key => $item) {
            $record = $this->model->where('name', $key)->where('group', 'vpay')->find();
            if (!empty($record)) {
                $record->save([
                    'value' => $item
                ]);
            } else {
                $this->model->insert([
                    'name'    => $key,
                    'type'  => 'string',
                    'group' => 'vpay',
                    'value' => $item
                ]);
            }
        }
        $this->success(__('保存成功'));  
    }
}